package com.heqiang.spring;

import com.heqiang.spring.context.support.ClassXmlApplicationContext;
import com.heqiang.spring.service.TestService;
import org.openjdk.jol.info.ClassLayout;

/**
 * 学习链接
 * https://mp.weixin.qq.com/s/npVKYqHVTDgYWa2Jq8PB-A
 */
public class TestMain {


    /***
     * 单例
     * @param args
     * @throws Exception
     */
    public static void main1(String[] args) throws Exception{

        String xml = "classpath:spring.xml";
        ClassXmlApplicationContext classXmlApplicationContext = new ClassXmlApplicationContext(xml);
        //钩子函数作用，当程序正常退出的时候，如果需要清理内存，回收资源的话，可以添加一个钩子函数，
        //用法：Runtime.getRuntime().addShutdownHook(Thread thread)
        //本质上是在jvm关闭时，执行一个线程
        classXmlApplicationContext.registerShutdownHok();

        TestService testService1 = classXmlApplicationContext.getBean("testService",TestService.class);
        TestService testService2 = classXmlApplicationContext.getBean("testService",TestService.class);


        System.out.println(testService1);
        System.out.println(testService2);

        System.out.println(testService1+"16进制哈希："+Integer.toHexString(testService1.hashCode()));
        System.out.println(ClassLayout.parseInstance(testService1).toPrintable());

    }



    public static void main(String[] args) throws Exception{

        String xml = "classpath:spring.xml";
        ClassXmlApplicationContext classXmlApplicationContext = new ClassXmlApplicationContext(xml);
        //钩子函数作用，当程序正常退出的时候，如果需要清理内存，回收资源的话，可以添加一个钩子函数，
        //用法：Runtime.getRuntime().addShutdownHook(Thread thread)
        //本质上是在jvm关闭时，执行一个线程
        classXmlApplicationContext.registerShutdownHok();

        TestService testService1 = classXmlApplicationContext.getBean("testService",TestService.class);


        System.out.println(testService1.queryInfo());

    }




}
